﻿package igz.fleaxy.ui.scrollbar 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * 
	 * Es un ScrollContainer con las barras de scroll integradas que automatiza
	 * el establecimiento de tamaños teniendo en cuenta si las barras de scroll
	 * estan visibles o no.
	 * 
	 * Los estilos usados para las dos barras de scroll son los mismos.
	 * 
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class ScrollBarContainerUi extends Sprite {
		
		protected var _ScrollContainerUi : ScrollContainerUi;
		public function get $ScrollContainerUi () : ScrollContainerUi {
			return _ScrollContainerUi;
		}
		
		protected var _ScrollBarV : ScrollBarV;
		public function get $ScrollBarV() : ScrollBarV {
			return _ScrollBarV;
		}
		
		protected var _ScrollBarH : ScrollBarH;
		public function get $ScrollBarH() : ScrollBarH {
			return _ScrollBarH;
		}
		
		private var _ContainerWidth  : uint;
		private var _ContainerHeight : uint;
		
		/**
		 * ScrollContainer con las barras de scroll integradas
		 * 
		 * @param	p_scrollContainerSettings { width : 100
		 * 					   		, height: 100
		 *					   		, background : 0xFFFFFF
		 *				   	   		, backgroundAlpha : 1
		 *					   		, autoscrollY : false
		 * 							}
		 * 
		 * @param	p_scrollBarStyles { backgroundStyle : "scrollbarBg", markStyle : "scrollbarMark" }
		 */
		public function ScrollBarContainerUi ( p_scrollContainerSettings:Object = null, p_scrollBarStyles:Object = null ) {
			_ScrollContainerUi = new ScrollContainerUi( p_scrollContainerSettings );
			super.addChild( _ScrollContainerUi );
			
			_ScrollBarV = new ScrollBarV( _ScrollContainerUi, p_scrollBarStyles );
			super.addChild( _ScrollBarV );
			
			_ScrollBarH = new ScrollBarH( _ScrollContainerUi, p_scrollBarStyles );
			super.addChild( _ScrollBarH );
			
			_ContainerWidth = _ScrollContainerUi.width;
			_ContainerHeight = _ScrollContainerUi.height;

			var timerRefreshScrollBars : Timer = new Timer(500);
			timerRefreshScrollBars.addEventListener(TimerEvent.TIMER, _RefreshScrollBars_OnTimer);
			timerRefreshScrollBars.start();

		}

		private function _RefreshScrollBars_OnTimer ( p_timerEvent:TimerEvent ) : void {
			_ScrollBarV.$Refresh();
			_ScrollBarH.$Refresh();
			_SetScrollContainerSize();
		}

		public function $Refresh():void {
			_SetScrollContainerSize();
		}
		
		private function _SetScrollContainerSize () : void {
			if ( _ScrollContainerUi.$Container.height > _ContainerHeight ) {
				_ScrollContainerUi.width  = _ContainerWidth - _ScrollBarV.width;
			}
			else {
				_ScrollContainerUi.width  = _ContainerWidth;
			}

			if ( _ScrollContainerUi.$Container.width > _ContainerWidth ) {
				_ScrollContainerUi.height = _ContainerHeight - _ScrollBarH.height;
			}
			else {
				_ScrollContainerUi.height = _ContainerHeight;
			}
		}
		
		override public function get width () : Number {
			return _ContainerWidth;
		}
		
		override public function set width ( p_width:Number ) : void {
			_ContainerWidth = p_width;
			_SetScrollContainerSize();
		}
		
		override public function get height () : Number {
			return _ContainerHeight;
		}
		
		override public function set height ( p_height:Number ) : void {
			_ContainerHeight = p_height;
			_SetScrollContainerSize();
		}
		
		override public function addChild ( p_child:DisplayObject ) : DisplayObject {
			_ScrollContainerUi.addChild( p_child );
			_SetScrollContainerSize();
			return p_child;
		}
		
		override public function removeChild ( p_child:DisplayObject ) : DisplayObject {
			return $ScrollContainerUi.removeChild( p_child );
		}
		
	}
	
}